(function(e) {
    e.PolyEditor = function(a, b) {
        this._map = a;
        this._poly = b
    };
    e.PolyEditor.prototype = {
        open: function() {
            this._markers || this._initMarkers();
            for (var a = 0, b = this._markers.length; a < b; a++) 
            	this._markers[a].setMap(this._map),
            this._markers[a]._middleRight && this._markers[a]._middleRight.setMap(this._map)
        },
        close: function() {
            if (this._markers) {
                for (var a = 0, b = this._markers.length; a < b; a++) 
                	this._markers[a].setMap(null),
                this._markers[a]._middleRight && this._markers[a]._middleRight.setMap(null);
                delete this._markers
            }
        },
        _initMarkers: function() {
            this._markers = [];
            this._lnglats = this._poly.getPath();
            var a,
            b,
            d;
            a = 0;
            for (d = this._lnglats.length; a < d; a++) 
            	b = this._createMarker(this._lnglats[a], a),
            	b.on("click", this._onMarkerClick, this),
            	this._markers.push(b);
            var c;
            a = 0;
            for (b = d - 1; a < d; b = a++) if (0 !== a || e.Polygon && this._poly instanceof e.Polygon) b = this._markers[b],
            c = this._markers[a],
            this._createMiddleMarker(b, c),
            this._updatePrevNext(b, c)
        },
        _createMarker: function(a, b) {
            var d = new e.Marker({
                position: a,
                draggable: !0,
                icon: new e.Icon({
                    size: new e.Size(11, 
                    11),
                    imageOffset: new e.Pixel(0, 0),
                    image: "http://webapi.amap.com/images/dd-via.png"
                }),
                offset: new e.Pixel( - 6, -6)
            });
            d._origLngLat = a;
            d._index = b;
            d.on("dragging", this._onMarkerDrag, this);
            d.on("dragend", this._fireEdit, this);
            return d
        },
        _removeMarker: function(a) {
            var b = a._index;
            a.setMap(null);
            this._markers.splice(b, 1);
            this._lnglats.splice(b, 1);
            this._poly.setPath(this._lnglats);
            this._updateIndexes();
            a.off("dragging", this._onMarkerDrag, this).off("dragend", this._fireEdit, this).off("click", this._onMarkerClick, this)
        },
        _fireEdit: function() {
            this._poly.edited = !0;
            this._poly.emit("edit")
        },
        _onMarkerDrag: function(a) {
            var b = a.target;
            b._middleLeft && b._middleLeft.setPosition(this._getMiddleLatLng(b._prev, b));
            b._middleRight && b._middleRight.setPosition(this._getMiddleLatLng(b, b._next));
            this._lnglats[b._index] = a.lnglat;
            this._poly.setPath(this._lnglats)
        },
        _onMarkerClick: function(a) {
            3 > this._lnglats.length || (a = a.target, this._removeMarker(a), this._updatePrevNext(a._prev, a._next), a._middleLeft && a._middleLeft.setMap(null), a._middleRight && 
            a._middleRight.setMap(null), a._prev && a._next ? this._createMiddleMarker(a._prev, a._next) : a._prev ? a._next || (a._prev._middleRight = null) : a._next._middleLeft = null, this._fireEdit())
        },
        _updateIndexes: function() {
            for (var a = 0, b = this._markers.length; a < b; a++) this._markers[a]._index = a
        },
        _createMiddleMarker: function(a, b) {
            var d = this._getMiddleLatLng(a, b),
            c = this._createMarker(d),
            e,
            f,
            g;
            a._middleRight = b._middleLeft = c;
            f = function() {
                var f = b._index;
                c._index = f;
                c.off("click", e, this).on("click", this._onMarkerClick, this);
                d = c.getPosition();
                this._lnglats.splice(f, 0, d);
                this._poly.setPath(this._lnglats);
                this._markers.splice(f, 0, c);
                this._updateIndexes();
                this._updatePrevNext(a, c);
                this._updatePrevNext(c, b)
            };
            g = function() {
                c.off("dragstart", f, this);
                c.off("dragend", g, this);
                this._createMiddleMarker(a, c);
                this._createMiddleMarker(c, b)
            };
            e = function() {
                f.call(this);
                g.call(this);
                this._fireEdit()
            };
            c.on("click", e, this).on("dragstart", f, this).on("dragend", g, this);
            c.setMap(this._map)
        },
        _updatePrevNext: function(a, b) {
            a && (a._next = b);
            b && (b._prev = a)
        },
        _getMiddleLatLng: function(a, 
        b) {
            var d = this._map,
            c = d.project(a.getPosition()),
            e = d.project(b.getPosition());
            return d.unproject(c.add(e).divideBy(2))
        }
    }
})(AMap);